/**
 * Author : wang885298.
 * Date   : 2018/6/21.
 * File   : post-proxy.接口的post请求
 */
import * as axios from 'axios'
// import { getErrorCn } from './errorMap'
import {isEmptyObject} from '../utils'
/**
 * 基于axios,自定义的post方法
 * @param url
 * @param data
 * @param sec
 * @returns {Promise}
 */
async function post (url, data) {
  // console.debug('请求参数：')
  // console.debug(data)
  let proxyUrl = ''
  let proxyHost = window.localStorage.getItem('proxyHost')
  proxyUrl = proxyHost + url
  // data = JSON.stringify(data)
  return new Promise((resolve, reject) => {
    axios.post(proxyUrl, data, {headers: {'Content-Type': 'application/json;charset=utf-8'}}).then(function (response) {
      if (!isEmptyObject(response.data)) {
        // 正常响应处理
        if (response.data.code === 0) {
          resolve(response.data.data)
        } else {
          reject(response.data.msg)
        }
      } else {
        resolve(response.data)
      }
    }).catch(function (error) {
      console.error('请求服务器发生错误：')
      console.error(error)
      reject(error)
    })
  })
}

async function get (url, data, sec) {
  // console.debug('请求参数：')
  // console.debug(data)
  let proxyUrl = ''
  let proxyHost = window.localStorage.getItem('proxyHost')
  proxyUrl = proxyHost + url
  // data = encodeURIComponent(JSON.stringify(data))
  return new Promise((resolve, reject) => {
    axios.get(proxyUrl, {params: data}).then(function (response) {
      if (!isEmptyObject(response.data)) {
        // 正常响应处理
        if (response.data.code === 0) {
          resolve(response.data.data)
        } else {
          reject(response.data.msg)
        }
      } else {
        resolve(response.data)
      }
    }).catch(function (error) {
      console.error('请求服务器发生错误：')
      console.error(error)
      reject(error)
    })
  })
}
export default post
export {
  post,
  get
}
